﻿using System.ComponentModel.Composition;
using System.Windows;
using ScrumFactory.Windows.Helpers;

namespace ScrumFactory.Windows
{

    [Export(typeof(IShellView))]
    public partial class MainWindow : Window, IShellView
    {
        private ViewModel.IShellViewModel model;

        protected DelayAction closeLeftPanelDelay;

        public MainWindow()
        {
            this.InitializeComponent();

            leftPanels.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(leftPanels_SelectionChanged);
            leftPanelsBorder.MouseLeave += new System.Windows.Input.MouseEventHandler(leftPanelsBorder_MouseLeave);

            leftGridSplitter.DragStarted += new System.Windows.Controls.Primitives.DragStartedEventHandler(leftGridSplitter_DragStarted);

            closeLeftPanelDelay = new DelayAction(1000, new DelayAction.ActionDelegate(() => { leftPanels.SelectedItem = null; }));
            
        }

        void leftGridSplitter_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e) {
            closeLeftPanelDelay.Stop();
        }


        void leftPanelsBorder_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e) {
            closeLeftPanelDelay.StartAction();
        }

        [Import()]
        public ViewModel.IShellViewModel Model
        {
            get
            {
                return this.model;
            }
            set
            {
                this.model = value;
                this.DataContext = value;
            }
        }

        void IShellView.Show()
        {
            this.Show();
        }


        
        private void leftPanels_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) {
            if (leftPanels.SelectedItem == null) {
                leftColumn.MinWidth = 0;
                leftColumn.Width = new GridLength(100, GridUnitType.Auto);
            } else {
                leftColumn.MinWidth = 250;
            }
        }

        private void bottomPanels_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) {
            if (bottomPanels.SelectedItem == null)
                bottomRow.Height = new GridLength(100, GridUnitType.Auto);
        }

              



        

    }
}
